﻿//-----------------------------------------------------------------------
// <copyright>
//     Copyright (c) Artur Mustafin. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace Expressions.Compiler.Internal
{
    /// <summary>
    /// A nonterminal
    /// </summary>
    internal sealed class Reference<T> : Entity<T>
    {
        private readonly Nonterminal<T> _nonterminal;

        internal Reference(Nonterminal<T> nonterminal, bool optional)
            : base(nonterminal, optional)
        {
            _nonterminal = nonterminal;
        }

        internal Nonterminal<T> Nonterminal
        {
            get
            {
                return _nonterminal;
            }
        }

        public override bool Parse(Parser<T> parser)
        {
            if (_nonterminal.Parse(parser))
            {
                return !Optional;
            }
            return Optional;
        }

#if TRACE
        internal override string Name
        {
            get
            {
                return _nonterminal.Name;
            }
        }  
        
        public override string ToString()
        {
            if (Optional)
            {
                return string.Format("({0})", _nonterminal);
            }
            return _nonterminal.ToString();
        }
#endif
    }

    /// <summary>
    /// A nonterminal
    /// </summary>
    internal sealed class Reference : Entity
    {
        private readonly Nonterminal _nonterminal;

        internal Reference(Nonterminal nonterminal, bool optional)
            : base(nonterminal, optional)
        {
            _nonterminal = nonterminal;
        }

        internal Nonterminal Nonterminal
        {
            get
            {
                return _nonterminal;
            }
        }

        public override bool Parse(Parser parser)
        {
            if (_nonterminal.Parse(parser))
            {
                return !Optional;
            }
            return Optional;
        }

#if TRACE
        internal override string Name
        {
            get
            {
                return _nonterminal.Name;
            }
        }
       
        public override string ToString()
        {
            if (Optional)
            {
                return string.Format("({0})", _nonterminal);
            }
            return _nonterminal.ToString();
        }
#endif
    }
}
